<?php
namespace app\api\model;

use think\Model;

use think\Cache;

class UserTransactionModel extends model{
	
	protected $table = 'ly_user_transaction';
	
	//资金明显 流水
	public function FundDetails(){
		//获取参数
		$token 			= input('post.token/s');
		$userArr		= explode(',',auth_code($token,'DECODE'));
		$uid			= $userArr[0];
		$trade_type		= input('post.trade_type/i');		// 流水类型 0=全部 1=转入 2=转出 3 = 冻结 4 = 解冻
		$lang			= (input('post.lang')) ? input('post.lang') : 'cn';	// 语言类型
		
		$param			= input('param.');
		
		//进行中的订单
		if($trade_type != 0){
			switch($trade_type){
				case 4://收入
					$where = array(['uid','=',$uid],['trade_type','in',array(5,6,7,8,10,15)]);
				break;
				case 3://支出
					$where = array(['uid','=',$uid],['trade_type','in',array(3,4,14,9)]);
				break;
			}

		}else{
			$where   = array(['uid','=',$uid]);
		}

		$count   =	model('TradeDetails')->where($where)->count();
		if(!$count){
			$data['code']		= 0;
			$data['code_dec']	= config($lang.'.lang')[45];	// 暫無數據
			return $data;
		}

		//每页显示记录
		$pageSize 			= (isset($param['page_size']) and $param['page_size']) ? $param['page_size'] : 10;
		//当前的页,还应该处理非数字的情况
		$pageNo 			= (isset($param['page_no']) and $param['page_no']) ? $param['page_no'] : 1;
		//总页数
		$pageTotal 			= ceil($count / $pageSize);//当前页数大于最后页数，取最后	
		//记录数
		$limitOffset 		= ($pageNo - 1) * $pageSize;
		
		$orderdata			= model('TradeDetails')->where($where)
							->field('id,trade_type,trade_amount,order_number,account_balance,trade_number,remarks,trade_time')
							->order('trade_time','DESC')
							//->limit($limitOffset, $pageSize)
							->select()->toArray();
			
		if(!$orderdata){
			$data['code']		= 0;
			$data['code_dec']	= config($lang.'.lang')[45];
			return $data;
		}

		$data						=	[];
		$data['code'] 				= 1;
		$data['data_total_nums'] 	= $count;
		$data['data_total_page'] 	= $pageTotal;
		$data['data_current_page'] 	= $pageNo;
		
		foreach($orderdata as $key =>$value){
			$data['list'][$key]['id']					= $value['id'];
			$data['list'][$key]['trade_amount']			= $value['trade_amount'];			// 金额
			$data['list'][$key]['trade_time']			= date("Y-m-d H:i:s",$value['trade_time']);	// 时间
			$data['list'][$key]['trade_type']			= $value['trade_type'];//类型
			$data['list'][$key]['trade_dec']			= config('custom.transactionType')[$value['trade_type']];//订单状态
			$data['list'][$key]['remarks']				= $value['remarks'];//说明	
			$data['list'][$key]['order_number']			= $value['order_number'];//订单号
			$data['list'][$key]['trade_number']			= $value['trade_number'];//流水号
			$data['list'][$key]['account_balance']		= $value['account_balance'];//余额
			switch($value['trade_type']){
				case 2:case 3:case 11:
					$data['list'][$key]['jj']			= '-';//余额
				break;
				default:
					$data['list'][$key]['jj']			= '+';//余额
			}
		}
		return $data;		
	}
	
	//转账
	public function Transfer(){		
		$param 			= input('param.');
		$userArr  		= explode(',',auth_code($param['token'],'DECODE'));
		$uid      		= $userArr[0];
		$lang		= (input('post.lang')) ? input('post.lang') : 'cn';	// 语言类型
		
		$username 		= (input('post.username')) ? input('post.username') : '';	// 转id
		$turn_money 	=	(input('post.turn_money')) ? input('post.turn_money') : 0;	// 金额
		$drawword 		=	(input('post.drawword')) ? input('post.drawword') : 0;	// 密码
		

		return ['code' => 0, 'code_dec' => config($lang.'.lang')[41]];	//失败//不开放

		if(!$username or !$turn_money or !$drawword){
			return ['code' => 0, 'code_dec' => config($lang.'.lang')[41]];	//失败
		}
		//本人
		$userinfo		= model('Users')->field('ly_users.id,ly_users.fund_password,ly_users.username,ly_users.sid,user_total.balance')->join('user_total','ly_users.id=user_total.uid')->where('ly_users.id', $uid)->find();
		if(!$userinfo){
			return ['code'=>0,'code_dec'=>config($lang.'.lang')[2]];	//用户不存在
		}
		
		//转给
		$tuserinfo		= model('Users')->field('ly_users.id,ly_users.fund_password,ly_users.username,ly_users.sid,user_total.balance')->join('user_total','ly_users.id=user_total.uid')->where('ly_users.username', $username)->find();

		if(!$tuserinfo){
			return ['code'=>0,'code_dec'=>config($lang.'.lang')[2]];
		}

		if($userinfo['username'] == $tuserinfo['username']){
			return ['code'=>0,'code_dec'=>config($lang.'.lang')[41]];
		}
		
		if($userinfo['balance'] < $turn_money){
			return ['code'=>2,'code_dec'=>config($lang.'.lang')[41]];	//失败
		}
		
		//检查资金密码
		if(auth_code($userinfo['fund_password'],'DECODE') != $drawword){
			$data['code']		= 6;
			$data['code_dec']	= config($lang.'.lang')[137];		//密码错误	
			return $data;
		}

		//减钱
		$is_up_to = model('UserTotal')->where('uid', $userinfo['id'])->setDec('balance', $turn_money);
		if(!$is_up_to){
			return ['code'=>0,'code_dec'=>config($lang.'.lang')[41]];
		}
		
		// 流水
		$financial_data['uid'] 					= $userinfo['id'];
		$financial_data['username'] 			= $userinfo['username'];
		$financial_data['order_number'] 		= 'Z'.trading_number();
		$financial_data['trade_number'] 		= 'L'.trading_number();
		$financial_data['trade_type'] 			= 11;
		$financial_data['trade_before_balance']	= $userinfo['balance'];
		$financial_data['trade_amount'] 		= $turn_money;
		$financial_data['account_balance'] 		= $userinfo['balance'] - $turn_money;
		$financial_data['remarks'] 				= config($lang.'.lang')[171];	//转账转出
		$financial_data['types'] 				= 1;	// 用户1，商户2

		model('common/TradeDetails')->tradeDetails($financial_data);
		
		//加钱
		model('UserTotal')->where('uid', $tuserinfo['id'])->setInc('balance', $turn_money);

		// 流水
		$financial_data_p['uid'] 					= $tuserinfo['id'];
		$financial_data_p['username'] 				= $tuserinfo['username'];
		$financial_data_p['order_number'] 			= 'Z'.trading_number();
		$financial_data_p['trade_number'] 			= 'L'.trading_number();
		$financial_data_p['trade_type'] 			= 12;
		$financial_data_p['trade_before_balance']	= $tuserinfo['balance'];
		$financial_data_p['trade_amount'] 			= $turn_money;
		$financial_data_p['account_balance'] 		= $tuserinfo['balance'] + $turn_money;
		$financial_data_p['remarks'] 				= config($lang.'.lang')[172];	//转账转入
		$financial_data_p['types'] 					= 1;	// 用户1，商户2

		model('common/TradeDetails')->tradeDetails($financial_data_p);
			
		return ['code'=>1, 'code_dec'=>config($lang.'.lang')[42]];
	}

	
}
